Windows Forms - Redes e Wifi
Imports System.Net.NetworkInformation
Module WiFi
Redes Wifi No Alcance
Public Function RedesWifiNoAlcance(detalhes As Boolean) As List(Of String)
Dim netsh As Process
Dim umaLinha As String
Dim linhaPartes() As String
Dim a As New List(Of String)
a.Clear()
' ----- Constroi e executa o comando
netsh = New Process()
netsh.StartInfo.FileName = "netsh"
netsh.StartInfo.Arguments = "wlan show networks mode=bssid"
'verifica se vai exibir todos os detalhes
If (detalhes = True) Then netsh.StartInfo.Arguments = "/All"
'inicia o processo
netsh.StartInfo.UseShellExecute = False
netsh.StartInfo.RedirectStandardOutput = True
netsh.StartInfo.CreateNoWindow = True
netsh.Start()
' ----- Processa cada linha de entrada
Do While Not netsh.StandardOutput.EndOfStream
' ----- Ignora linhas em branco.
umaLinha = netsh.StandardOutput.ReadLine()
If (Trim(umaLinha) = "") Then Continue Do
' ----- Cabeçalho não possui espaços em branco
If (umaLinha = umaLinha.TrimStart) Or (InStr(umaLinha, ":") = 0) Then
a.Add(umaLinha.Trim)
Else
' ----- Uma linha de detalhe. O formato é:
' Titulo ... : Dados
linhaPartes = umaLinha.Trim.Split(":"c)
linhaPartes(0) = Replace(linhaPartes(0), ". ", "")
linhaPartes(1) = linhaPartes(1).Trim
a.Add(vbTab & linhaPartes(0) & ":" & linhaPartes(1))
End If
Loop
netsh.WaitForExit()
netsh.Dispose()
Return a
End Function
Retorna o endereço MAC dos pontos na rede
Private Function RetornaEnderecoMac() As List(Of String)
Dim a As New List(Of String)
'precisa da referencia ao System.Management
' project, add reference, Assemblies, Framework,System.Management
Dim mc As System.Management.ManagementClass
Dim mo As System.Management.ManagementBaseObject
a.Clear()
mc = New Management.ManagementClass("Win32_NetworkAdapterConfiguration")
Dim moc As Management.ManagementObjectCollection = mc.GetInstances
For Each mo In moc
If mo.Item("IPenabled") = True Then
a.Add(mo.Item("MacAddress"))
End If
Next
Return a
End Function
Placas e adapatadores Wifi da Rede Local
Public Function RedesLocais() As List(Of String)
Dim a As New List(Of String)
'precisa de Imports System.Net.NetworkInformation
Dim adapters As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
Dim n As NetworkInterface
a.Clear()
For Each n In adapters
a.Add(n.Name) ' + vbTab + n.OperationalStatus)
Next n
Return a
End Function
Obter SSID do WiFi
Public Function ObterSSID() As List(Of String)
Dim a As New List(Of String)
Try
Dim query As String = "SELECT * FROM MSNDis_80211_BSSIList WHERE Active= 'True'"
Dim searcher As Management.ManagementObjectSearcher = New Management.ManagementObjectSearcher("root/WMI", query)
Dim moc As Management.ManagementObjectCollection = searcher.Get()
Dim moe As Management.ManagementObjectCollection.ManagementObjectEnumerator = moc.GetEnumerator()
moe.MoveNext()
Dim objarr() As Management.ManagementBaseObject = CType(moe.Current.Properties("Ndis80211BSSIList").Value, Management.ManagementBaseObject())
a.Clear()
For Each obj As Management.ManagementBaseObject In objarr
Dim ssid() As Char = System.Text.Encoding.ASCII.GetChars(CType(obj("Ndis80211Ssid"), Byte()))
a.Add(New String(ssid))
Next
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Exclamation)
End Try
Return a
End Function
Obter o nível de sinal do Wifi
Public Function ObterNivelSinal() As List(Of String)
Dim a As New List(Of String)
a.Clear()
Dim query As Management.ManagementObjectSearcher
Dim Qc As Management.ManagementObjectCollection
Dim Oq As Management.ObjectQuery
Dim Ms As Management.ManagementScope
Dim Co As Management.ConnectionOptions
Dim Mo As Management.ManagementObject
Dim signalStrength As Double
Try
Co = New Management.ConnectionOptions
Ms = New Management.ManagementScope("root\wmi")
Oq = New Management.ObjectQuery("SELECT * FROM MSNdis_80211_ReceivedSignalStrength Where active=true")
query = New Management.ManagementObjectSearcher(Ms, Oq)
Qc = query.Get
signalStrength = 0
For Each Mo In query.Get
signalStrength = Convert.ToDouble(Mo("Ndis80211ReceivedSignalStrength"))
a.Add(signalStrength.ToString)
Next
Catch exp As Exception
Return Nothing ' Indica sem sinal wifi - pode ser cabo por exemplo
End Try
Return a
End Function
End Module